package cn.xopencode.recordservice.service.maintransformer;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.IdUtil;
import cn.xopencode.common.framework.exception.util.ServiceExceptionUtil;
import cn.xopencode.common.framework.util.DateUtil;
import cn.xopencode.common.framework.vo.PageResult;
import cn.xopencode.recordservice.convert.maintransformer.MainTransformerRecordConvert;
import cn.xopencode.recordservice.convert.maintransformer.MainTransformerRecordEmpRelConvert;
import cn.xopencode.recordservice.convert.robotroom.AdminRecordConvert;
import cn.xopencode.recordservice.dal.mysql.dataobject.maintransformer.MainTransformerRecordDO;
import cn.xopencode.recordservice.dal.mysql.dataobject.maintransformer.MainTransformerRecordEmpRelDO;
import cn.xopencode.recordservice.dal.mysql.dataobject.pdroom.PowerDistributionRoomDO;
import cn.xopencode.recordservice.dal.mysql.mapper.maintransformer.MainTransformerRecordEmpRelMapper;
import cn.xopencode.recordservice.dal.mysql.mapper.maintransformer.MainTransformerRecordMapper;
import cn.xopencode.recordservice.dal.mysql.mapper.pdroom.PowerDistributionRoomMapper;
import cn.xopencode.recordservice.rpc.maintransformer.vo.MainTransformerRecordAdminPageVO;
import cn.xopencode.recordservice.rpc.maintransformer.vo.MainTransformerRecordPageVO;
import cn.xopencode.recordservice.rpc.maintransformer.vo.MainTransformerRecordUpdateVO;
import cn.xopencode.recordservice.service.maintransformer.bo.MainTransformerRecordCreateBO;
import cn.xopencode.recordservice.service.maintransformer.bo.MainTransformerRecordPageBO;
import cn.xopencode.recordservice.service.maintransformer.bo.MainTransformerRecordUpdateBO;
import cn.xopencode.recordservice.service.robotroom.bo.AdminRecordPageBO;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

import static cn.xopencode.recordservice.enums.RecordErrorCodeConstants.RECORD_ROBOT_ROOM_NOT_EXCEPTION;
import static cn.xopencode.recordservice.enums.RecordErrorCodeConstants.RECORD_ROBOT_ROOM_NOT_EXITS;

/**
 * 配电室管理 service
 */
@Service
@Validated
public class MainTransformerRecordService {
    @Autowired
    private MainTransformerRecordMapper mainTransformerRecordMapper;

    @Autowired
    private MainTransformerRecordEmpRelMapper mainTransformerRecordEmpRelMapper;
    @Autowired
    private PowerDistributionRoomMapper powerDistributionRoomMapper;

    public Integer createMainTransformerRecord(MainTransformerRecordCreateBO createBO, Set<String> empIds){
        MainTransformerRecordDO recordDO = MainTransformerRecordConvert.INSTANCE.convert(createBO);
        String roomId = IdUtil.objectId();
        recordDO.setId(roomId);
        Integer result = mainTransformerRecordMapper.insert(recordDO);
        //保存关联的班组信息
        if (!CollectionUtil.isEmpty(empIds)) {
            List<MainTransformerRecordEmpRelDO> mainTransformerRecordEmpRelDOList = empIds.stream()
                    .map(empId -> new MainTransformerRecordEmpRelDO().setId(IdUtil.objectId()).setRecordId(roomId).setEmpId(empId)).collect(Collectors.toList());
            mainTransformerRecordEmpRelMapper.insertList(mainTransformerRecordEmpRelDOList);
        }
        return result;
    }

    public PageResult<MainTransformerRecordPageVO> pageMainTransformerRecord(MainTransformerRecordPageBO pageBO) {
        IPage<MainTransformerRecordDO> roomDOIPage =  mainTransformerRecordMapper.selectPage(pageBO);
        PageResult<MainTransformerRecordPageVO> boPageResult = MainTransformerRecordConvert.INSTANCE.convertPage(roomDOIPage);
        if(!CollectionUtil.isEmpty(boPageResult.getList())){
            List<MainTransformerRecordPageVO> list = boPageResult.getList();
            for(MainTransformerRecordPageVO roomVO : list){
                List<MainTransformerRecordEmpRelDO> relDOList = mainTransformerRecordEmpRelMapper.selectListByRecordId(roomVO.getId());
                List<String> tmp = relDOList.stream().map(MainTransformerRecordEmpRelDO::getEmpId).collect(Collectors.toList());
                roomVO.setEmpIds(String.join(",", tmp));
            }
        }
        return boPageResult;
    }

    public MainTransformerRecordUpdateVO getMainTransformerRecordById(String id){
        MainTransformerRecordDO roomDO = mainTransformerRecordMapper.selectById(id);
        if(roomDO != null){
            MainTransformerRecordUpdateVO roomVO = MainTransformerRecordConvert.INSTANCE.convertUpdate(roomDO);
            //查询所有关联的巡检组
            List<MainTransformerRecordEmpRelDO> relDOList = mainTransformerRecordEmpRelMapper.selectListByRecordId(id);
            roomVO.setEmpList(MainTransformerRecordEmpRelConvert.INSTANCE.convertList2(relDOList));

            PowerDistributionRoomDO pdRoomDO = powerDistributionRoomMapper.selectById(roomDO.getRoomId());
            if(pdRoomDO != null)
                roomVO.setRoomName(pdRoomDO.getRoomName());
            return roomVO;
        }
        return null;
    }

    public Integer updateMainTransformerRecord(MainTransformerRecordUpdateBO updateBO, Set<String> empIds){
        MainTransformerRecordDO roomDO = MainTransformerRecordConvert.INSTANCE.convert(updateBO);
        Integer result = mainTransformerRecordMapper.updateById(roomDO);

        //先删除所有关联的班组
        mainTransformerRecordEmpRelMapper.deleteByPdRecordId(roomDO.getId());

        //保存关联的班组信息
        if (!CollectionUtil.isEmpty(empIds)) {
            List<MainTransformerRecordEmpRelDO> mainTransformerRecordEmpRelDOList = empIds.stream()
                    .map(empId -> new MainTransformerRecordEmpRelDO().setId(IdUtil.objectId()).setRecordId(roomDO.getId()).setEmpId(empId)).collect(Collectors.toList());
            mainTransformerRecordEmpRelMapper.insertList(mainTransformerRecordEmpRelDOList);
        }
        return result;
    }


    public Integer dealMainTransformerRecordException(String recordId){
        MainTransformerRecordDO recordDO = mainTransformerRecordMapper.selectById(recordId);
        if(recordDO == null){
            throw ServiceExceptionUtil.exception(RECORD_ROBOT_ROOM_NOT_EXITS);
        }
        if(recordDO != null && recordDO.getNoticeLeader() != true){
            throw ServiceExceptionUtil.exception(RECORD_ROBOT_ROOM_NOT_EXCEPTION);
        }
        recordDO.setLeaderDeal(true);
        return mainTransformerRecordMapper.updateById(recordDO);
    }

    /**
     * 后台--根据查询条件 分页获取所有的主变巡检记录
     * @param pageBO
     * @return
     */
    public PageResult<MainTransformerRecordAdminPageVO> getAdminPageMainTransformerRecord(AdminRecordPageBO pageBO){
        Page<MainTransformerRecordDO> page = new Page<>(pageBO.getPageNo(),pageBO.getPageSize());
        if(pageBO.getInspectionTime() != null){
            String date = DateUtil.format(pageBO.getInspectionTime(), "yyyy-MM-dd");
            pageBO.setInspectionTime1(date.concat(" 00:00:00"));
            pageBO.setInspectionTime2(date.concat(" 23:59:59"));
        }
        IPage<MainTransformerRecordDO> recordDOList = mainTransformerRecordMapper.adminQuery(page,pageBO);
        return AdminRecordConvert.INSTANCE.convert5(recordDOList);
    }
}
